<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<%-- <%@taglib uri="spring.tld" prefix="spring"%> --%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String servletPath = (String)request.getAttribute("javax.servlet.forward.servlet_path");
String[] list = servletPath.split("\\/");
request.setAttribute("role",list[1]);
request.setAttribute("topMenuId",list[2]);
request.setAttribute("leftMenuId",list[3]);
%>

<!DOCTYPE html>
<html>
	<head>
		<base href="<%=basePath%>">
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<title>一键组卷管理</title>
		<meta name="keywords" content="">
		<link rel="shortcut icon" href="<%=basePath%>resources/images/favicon.ico" />
		<link href="resources/bootstrap/css/bootstrap-huan.css" rel="stylesheet">
		<link href="resources/font-awesome/css/font-awesome.min.css" rel="stylesheet">
		<link href="resources/css/style.css" rel="stylesheet">
		
		<link href="resources/css/exam.css" rel="stylesheet">
		<link href="resources/chart/morris.css" rel="stylesheet">
		<style>
			.change-property, .publish-paper, .delete-paper, .offline-paper{
				cursor:pointer;
			}
		</style>
	</head>
	<body>
		<header>
			<span style="display:none;" id="rule-role-val"><%=list[1]%></span>
			<div class="container">
				<div class="row">
					<jsp:include page="common/title.jsp"></jsp:include>
					<div class="col-xs-7" id="login-info">
						<c:choose>
							<c:when test="${not empty sessionScope.SPRING_SECURITY_CONTEXT.authentication.principal.username}">
								<div id="login-info-user">
									
									<a href="user-detail/${sessionScope.SPRING_SECURITY_CONTEXT.authentication.principal.username}" id="system-info-account" target="_blank">${sessionScope.SPRING_SECURITY_CONTEXT.authentication.principal.username}</a>
									<span>|</span>
									<a href="j_spring_security_logout"><i class="fa fa-sign-out"></i> 退出</a>
								</div>
							</c:when>
							<c:otherwise>
								<a class="btn btn-primary" href="user-register">用户注册</a>
								<a class="btn btn-success" href="user-login-page">登录</a>
							</c:otherwise>
						</c:choose>
					</div>
				</div>
			</div>
		</header>
		<!-- Navigation bar starts -->

		<div class="navbar bs-docs-nav" role="banner">
			<div class="container">
				<nav class="collapse navbar-collapse bs-navbar-collapse" role="navigation">
					<c:import url="/common-page/top-menu?topMenuId=${topMenuId}&leftMenuId=${leftMenuId}" charEncoding="UTF-8" />
				</nav>
			</div>
		</div>

		<!-- Navigation bar ends -->

		<!-- Slider starts -->

		<div>
			<!-- Slider (Flex Slider) -->

			<div class="container" style="min-height:500px;">

				<div class="row">
					<div class="col-xs-2" id="left-menu">
						<c:import url="/common-page/left-menu?topMenuId=${topMenuId}&leftMenuId=${leftMenuId}" charEncoding="UTF-8" />
					</div>
					<div class="col-xs-10" id="right-content">
						<div class="page-header">
							<h1><i class="fa fa-list-ul"></i> 一键组卷管理 </h1>
						</div>
						<div class="page-content">
							<form id="form-exampaper-add">
								<div class="form-line add-update-pass-point">
									<span class="form-label"><span class="warning-label">*</span>及格分数：</span>
									<input type="text" class="df-input-narrow" value="${autoCPaper.pass_point }">
									<input type="text" class="df-input-narrow hidden" id="typenum" value="${autoCPaper.type_num }">
									<input type="text" class="df-input-narrow hidden" id="autoCPaperid" value="${autoCPaper.id }">
									<span class="form-message" ></span>
								</div>
								<div class="form-line add-update-duration">
									<span class="form-label"><span class="warning-label">*</span>时长/分钟：</span>
									<input type="text" class="df-input-narrow" value="${autoCPaper.time }">
									<span class="form-message"></span>
								</div>
								<div class="form-line add-update-types" style="display: block;">
									<span class="form-label"><span class="warning-label">*</span>题型：</span>
									<fieldset>
										<legend>
											选择题型分布
										</legend>
										<span class="add-ques-type">
											<label >单选题</label>
											<input type="hidden" class="ques-id" value="1">
											<input type="text" class="df-input-narrow add-ques-amount">
											<span>道</span>
											<label >每道题</label>
											<input type="text" class="df-input-narrow add-ques-score">
											<span>分</span>
										</span>
										<br>
										<span class="add-ques-type">
											<label >多选题</label>
											<input type="hidden" class="ques-id" value="2">
											<input type="text" class="df-input-narrow add-ques-amount">
											<span>道</span>
											<label >每道题</label>
											<input type="text" class="df-input-narrow add-ques-score">
											<span>分</span>
										</span>
										<br>
										<span class="add-ques-type">
											<label >判断题</label>
											<input type="hidden" class="ques-id" value="3">
											<input type="text" class="df-input-narrow add-ques-amount">
											<span>道</span>
											<label >每道题</label>
											<input type="text" class="df-input-narrow add-ques-score">
											<span>分</span>
										</span>
									</fieldset>
									<span class="form-message"></span>
								</div>
								<div class="form-line add-total-point">
									<span class="form-label"><span class="warning-label">*</span>总分：</span>
									<input id="total-point" type="text" class="df-input-narrow" disabled="disbaled" value="${autoCPaper.total_point}">
									<span class="form-message"></span>
								</div>
								<button type="submit" class="btn btn-success df-submit">保存</button>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>

		<footer>
			<jsp:include page="footer.jsp"></jsp:include>

		</footer>

		<!-- Slider Ends -->

		<!-- Javascript files -->
		<!-- jQuery -->
		<script type="text/javascript" src="resources/js/jquery/jquery-1.9.0.min.js"></script>
		<script type="text/javascript" src="resources/js/all.js"></script>
		<!-- Bootstrap JS -->
		<script type="text/javascript" src="resources/bootstrap/js/bootstrap.min.js"></script>
		<script type="text/javascript" src="resources/js/exampaper-list.js?v=1"></script>
		<script type="text/javascript">

		$(function(){
			autoCPaper.init();
			autoCPaper.bindChangeAmount();
			autoCPaper.savebtnBind();
		});
		var autoCPaper = {
				init : function(){
					var typeNumber = $("#typenum").val();
					if(typeNumber.indexOf(",")>0){
						$(typeNumber.split(',')).each(function(i, item){
						 var jtem = item.split(':');
						 autoCPaper.findQType(jtem[0],jtem[1],jtem[2]);
						});
					}else{
						var jtem = typeNumber.split(':');
						autoCPaper.findQType(jtem[0],jtem[1],jtem[2]);
					}
				},
				findQType : function(type,count,score){
					$(".add-ques-type").each(function(i, item){
						if($(item).find(".ques-id").val()==type){
							$(item).find(".add-ques-amount").val(count);
							$(item).find(".add-ques-score").val(score);
						}
					});
				},
				calculateTotalPoints : function(){
					var qt = $(".add-ques-type");
					var amount = 0;
					for(var i = 0 ; i< qt.length;i++){
						var itemamount = parseInt($(qt[i]).find(".add-ques-amount").val());
						var itemscore = parseFloat($(qt[i]).find(".add-ques-score").val());
						
						if(isNaN(itemamount)||isNaN(itemscore)){
							continue;
						}else{
							amount = amount +  itemamount * itemscore * 10;
						}
					}
					$(".add-total-point input").val(amount / 10);
				},
				bindChangeAmount : function(){
					$(".add-ques-amount").change(function(){
						autoCPaper.calculateTotalPoints();
					});
					$(".add-ques-score").change(function(){
						autoCPaper.calculateTotalPoints();
					});
				},
				composeEntity : function() {
					var paperParam = new Object();
					paperParam.pass_point = parseInt($(".add-update-pass-point input").val());
					paperParam.time = $(".add-update-duration input").val();
					paperParam.total_point = $("#total-point").val();
					paperParam.id = $("#autoCPaperid").val();
					
					var qt = $(".add-ques-type");
					var amountMap = new Object();
					var pointMap = new Object();
					var type_num = [];
					for(var i = 0 ; i< qt.length;i++){
						var itemamount = parseInt($(qt[i]).find(".add-ques-amount").val());
						var itemscore = parseFloat($(qt[i]).find(".add-ques-score").val());
						var itemsid = $(qt[i]).find(".ques-id").val();
						if(isNaN(itemamount)||isNaN(itemscore)){
							continue;
						}else{
							amountMap[itemsid] = itemamount+":"+itemscore;
							pointMap[itemsid] = itemscore;
							type_num.push(itemsid+":"+itemamount+":"+itemscore);
						}
					}
					paperParam.type_num = type_num.join(",");
				return paperParam;
				},

				savebtnBind : function(){
					$("#form-exampaper-add").submit(function() {
						var question_entity = autoCPaper.composeEntity();
						$(".df-submit").attr("disabled","disabled");
						$.ajax({
							headers : {
								'Accept' : 'application/json',
								'Content-Type' : 'application/json'
							},
							type : "POST",
							url : util.getCurrentRole() + "/exam/edit-auotcpaper",
							data : JSON.stringify(question_entity),
							success : function(message, tst, jqXHR) {
								if (!util.checkSessionOut(jqXHR))
									return false;
								if (message.result == "success") {
									util.success("添加成功", function() {
										document.location.reload();
									});
								} else {
									util.error("操作失败请稍后尝试:" + message.result);
									$(".df-submit").removeAttr("disabled");
								}
	
							},
							error : function(jqXHR, textStatus) {
								util.error("操作失败请稍后尝试");
								$(".df-submit").removeAttr("disabled");
							}
						});
						return false;
					});
				}
			};
		
		</script>
	</body>
</html>